package laba7.taskA;

import interfaces.task7.simple.NamePrinterThread;
import java.io.PrintStream;

/**
 *
 * @author yakimovich
 */
public class NamePrinterThreadExt extends NamePrinterThread {

    private String name;
    private PrintStream stream;
    private long interval;
    private int count;

    /**
     * Create NamePrinterThreadExt by default.
     */
    public NamePrinterThreadExt() {
    }

    /**
     * Create NamePrinterThreadExt с with the specified parameters.
     *
     * @param name name for printing
     * @param stream stream for printing name
     * @param interval interval for printing name
     * @param count the number of printed name
     */
    public NamePrinterThreadExt(String name,
            PrintStream stream, long interval, int count) {
        this.name = name;
        this.stream = stream;
        this.interval = interval;
        this.count = count;
    }

    /**
     * Set a name for a printing.
     *
     * @param string the name for printing
     */
    @Override
    public void setPrintName(String string) {
        if (string == null) {
            throw new NullPointerException("The name can not be null");
        }
        if (string.equals("")) {
            throw new IllegalArgumentException("The name should not be empty");
        }
        this.name = string;
    }

    /**
     * Sets the output stream.
     *
     * @param stream PrintStream for printing
     */
    @Override
    public void setStream(PrintStream stream) {
        if (stream == null) {
            throw new NullPointerException("The stream can not be null");
        }
        this.stream = stream;
    }

    /**
     * Sets the interval for the printed word.
     *
     * @param l interval for the printed word
     */
    @Override
    public void setInterval(long l) {
        if (l <= 0) {
            throw new IllegalArgumentException("Incorrect value of interval");
        }
        this.interval = l;
    }

    /**
     * Sets the number of printed words.
     *
     * @param i the number of printed words
     */
    @Override
    public void setCount(int i) {
        if (i <= 0) {
            throw new IllegalArgumentException("Incorrect value of interval");
        }
        this.count = i;
    }

    @Override
    public void run() {
        for (int i = 0; i < count; i++) {
            try {
                Thread.sleep(interval);
            } catch (InterruptedException ex) {
                stream.println(ex);
            }
            System.setOut(stream);
            System.out.println(name);
        }
    }
}
